Crate loggerv [−] [src]
A simple io::stdout
and io::stderr
writing Logger
implementation from the
log
crate, using the ansi_term
crate for colors and configured at runtime via a verbosity
or at compile time with simple function calls. Designed for simple Command Line Interfaces
(CLIs).
This library includes a Builder pattern API for configuring a logger and three initializing helper functions to create a default logger. Ensure you create and initialize only once a global logger with the Builder pattern API or use one of the three public helper functions early in your program as shown in the examples below.
The default configuration colorizes the "tag" portion of the log statement, where the tag is
the text to the left of a separator, defaulted as the colon (:
). The message is the
portion to the right of the separator and it is not ever colorized. The tag includes only the
module path and the separator by default.
Example
The standard example with clap as the arg parser using the default configuration.
#[macro_use] extern crate log; extern crate clap; extern crate loggerv; use clap::{Arg, App}; fn main() { let args = App::new("app") .arg(Arg::with_name("v") .short("v") .multiple(true) .help("Sets the level of verbosity")) .get_matches(); loggerv::init_with_verbosity(args.occurrences_of("v")).unwrap(); error!("this is always printed"); warn!("this too, and it's printed to stderr"); info!("this is optional"); // for ./app -v or higher debug!("this is optional"); // for ./app -vv or higher trace!("this is optional"); // for ./app -vvv }
But obviously use whatever argument parsing methods you prefer.
Example
For a compile time switch, all you really need is log
(for the macros) and loggerv
for how
to print what's being sent to the macros with the default configuration.
#[macro_use] extern crate log; extern crate loggerv; use log::LogLevel; fn main() { loggerv::init_with_level(LogLevel::Info).unwrap(); debug!("this is a debug {}", "message"); error!("this is printed by default"); }
Example
If you don't really care at all you could just use the plain init_quiet
function to only show
warnings and errors with the default configuration:
#[macro_use] extern crate log; extern crate loggerv; fn main() { loggerv::init_quiet().unwrap(); info!("hidden"); error!("this is printed by default"); }
Example
If you want to configure the output, the Builder pattern API can be used.
#[macro_use] extern crate log; extern crate clap; extern crate loggerv; use clap::{Arg, App}; fn main() { let args = App::new("app") .arg(Arg::with_name("v") .short("v") .multiple(true) .help("Sets the level of verbosity")) .get_matches(); // This will change the log configuration to include line numbers in the "tag" portion of // the statement, which is the text to the left of the separator, change the separator from // a colon to an equal sign, hide or disable the module path, and disable colorized output. // See the Logger documentation for more configuration methods and the ability to change // the colors for each log level. loggerv::Logger::new() .verbosity(args.occurrences_of("v")) .level(true) .line_numbers(true) .separator(" = ") .module_path(false) .colors(false) .init() .unwrap(); error!("this is always printed"); warn!("this too, and it's printed to stderr"); info!("this is optional"); // for ./app -v or higher debug!("this is optional"); // for ./app -vv or higher trace!("this is optional"); // for ./app -vvv }
See the documentation for the log crate for more information about its API.
Structs
Logger |
Constants
Functions
init_quiet |
Initializes loggerv with only warnings and errors. |
init_with_level |
Initialize loggerv with a maximal log level. |
init_with_verbosity |
Initialize loggerv with a verbosity level. |